home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 476-500 / disk_500 / wiconify / wiconcalls.lzh / wIconCalls.c < prev    next >
C/C++ Source or Header  |  1991-04-19  |  15KB  |  729 lines

  1. /*
  2.  *  WICONIFY    A utility that allows you to iconify any Intuition window
  3.  *              on any screen, and to open WB windows on any screen.
  4.  *
  5.  *  wIconCalls.c    The programmer's interface to wIconify.  This module
  6.  *                  handles the inter-process communication between
  7.  *                  wIconify and client programs.
  8.  *
  9.  *  Copyright 1990 by Davide P. Cervone, all rights reserved.
  10.  *  You may use this code, provided this copyright notice is kept intact.
  11.  */
  12.  
  13. #define INTUITION_PREFERENCES_H             /* don't need 'em */
  14. #include <intuition/intuition.h>
  15. #include "wStructs.h"
  16.  
  17. /*
  18.  *  The version of wIconCalls.c
  19.  */
  20.  
  21. #define MAJVERSION      1
  22. #define MINVERSION      4
  23.  
  24. /*
  25.  *  The expected version of wIconify itself
  26.  */
  27.  
  28. #define MAJLIBVERS      3
  29. #define MINLIBVERS      7
  30.  
  31.  
  32. #define NEWICONIFY      ((struct MsgPort *)-1)
  33.  
  34. static struct MsgPort *wIconPort;           /* the wIconify message port */
  35. extern struct MsgPort *FindPort();
  36.  
  37. extern struct Window *wBackDropOf();        /* defined later on */
  38.  
  39.  
  40.  
  41. /*
  42.  *  ContactIconify()
  43.  *
  44.  *  If we have already got a pointer to wIconify, and the new one is not
  45.  *    the same as the old one, indicate that a new wIconify is running.
  46.  *  Otherwise, if we found a pointer to wIconify,
  47.  *    Get a reply port.
  48.  *    Set up a contact message for the new wIconify, and make contact.
  49.  *    Wait for a reply and then remove the port.
  50.  *    If there is a version mismatch, clear the pointer to wIconify.
  51.  *  Return the (possibly modified) pointer to wIconify.
  52.  */
  53.  
  54. static void ContactIconify(NewIconPort)
  55. struct MsgPort *NewIconPort;
  56. {
  57.    struct wIconMessage theMessage;
  58.    struct MsgPort *thePort;
  59.    extern struct wIconMessage *WaitPort();
  60.    extern struct MsgPort *CreatePort();
  61.  
  62.    if (wIconPort && NewIconPort != wIconPort)
  63.    {
  64.       NewIconPort = NEWICONIFY;
  65.    } else if (NewIconPort) {
  66.       thePort = CreatePort(NULL,0);
  67.       if (thePort)
  68.       {
  69.          theMessage.Window = NULL;
  70.          theMessage.Icon   = NULL;
  71.          theMessage.Action = WI_MAKECONTACT;
  72.          theMessage.Flags  = 0;
  73.          theMessage.Message.mn_ReplyPort = thePort;
  74.          theMessage.Message.mn_Length = sizeof(struct wIconMessage);
  75.          PutMsg(NewIconPort,&theMessage);
  76.          WaitPort(thePort);
  77.          DeletePort(thePort);
  78.          if (theMessage.Action != WI_VERSIONOK ||
  79.              theMessage.Data.Version.Maj < MAJLIBVERS ||
  80.             (theMessage.Data.Version.Maj == MAJLIBVERS &&
  81.              theMessage.Data.Version.Min < MINLIBVERS))
  82.                   NewIconPort = NULL;
  83.       }
  84.    }
  85.    wIconPort = NewIconPort;
  86. }
  87.  
  88.  
  89. /*
  90.  *  wIconifyActive()
  91.  *
  92.  *  Get a pointer to the current wIconify port.
  93.  *  If it's not the one we used to have, try to contact the new wIconify.
  94.  *  return a success status.
  95.  */
  96.  
  97. int wIconifyActive()
  98. {
  99.    struct MsgPort *NewIconPort;
  100.  
  101.    NewIconPort = FindPort(WICONPORT);
  102.    if (NewIconPort != wIconPort) ContactIconify(NewIconPort);
  103.    return(wIconPort && wIconPort != NEWICONIFY);
  104. }
  105.  
  106.  
  107. /*
  108.  *  *SendMessage()
  109.  *
  110.  *  Initializes and sends a message to wIconify.  The data items are
  111.  *  stored in the proper union fields, and the result from wIconify is
  112.  *  returned.
  113.  *
  114.  *  If wIconify is running,
  115.  *    Create a reply port, and initialize the message.
  116.  *    Set the data field for the proper message class.
  117.  *    Send the message to wIconify and wait for the reply.
  118.  *    Remove the reply port.
  119.  *  Massage the return data, and return the icon.
  120.  */
  121.  
  122. static WICONREF *SendMessage(theAction,theWindow,theIcon,Data1,Data2)
  123. struct Window *theWindow;
  124. WICONREF *theIcon;
  125. long Data1,Data2;
  126. {
  127.    struct wIconMessage theMessage;
  128.    struct MsgPort *thePort;
  129.    extern struct wIconMessage *WaitPort();
  130.    extern struct MsgPort *CreatePort();
  131.    extern APTR FindTask();
  132.  
  133.    theMessage.Icon = NULL;
  134.    if (wIconifyActive())
  135.    {
  136.       thePort = CreatePort(NULL,0);
  137.       if (thePort)
  138.       {
  139.          theMessage.Window = theWindow;
  140.          theMessage.Icon   = theIcon;
  141.          theMessage.Action = theAction;
  142.          theMessage.Flags  = 0;
  143.          switch(theAction)
  144.          {
  145. #ifndef WINDOW_CALLS
  146. #ifndef ICONSETTER_CALLS
  147. #ifndef SCREEN_CALLS
  148.             case WI_SELECTICON:
  149.                if (Data1) theMessage.Flags = WI_ADDTOSELECT;
  150.                break;
  151.  
  152.             case WI_MOVEICON:
  153.                theMessage.Data.Position.x = (WORD) Data1;
  154.                theMessage.Data.Position.y = (WORD) Data2;
  155.                break;
  156.  
  157.             case WI_REDRAW:
  158.                theMessage.Data.wScreen = (WSCREEN *) Data1;
  159.                break;
  160.  
  161.             case WI_UPDATEICON:
  162.                theMessage.Data.Icon = (WICON *) Data1;
  163.                break;
  164.  
  165.             case WI_ADDICON:
  166.             case WI_SELECTNEXT:
  167.             case WI_OPENSELECT:
  168.             case WI_CLOSESELECT:
  169. #endif
  170.             case WI_MAKEWB:
  171. #endif
  172. #endif
  173.             case WI_BACKDROPOF:
  174.                theMessage.Data.Screen = (struct Screen *) Data1;
  175.                break;
  176.  
  177. #ifndef WINDOW_CALLS
  178. #ifndef ICONSETTER_CALLS
  179.             case WI_NEWSCREEN:
  180.                theMessage.Data.NewScreen.Depth = Data1;
  181.                theMessage.Data.NewScreen.Modes = Data2;
  182.                break;
  183.  
  184.             case WI_OPENON:
  185.                theMessage.Data.OpenOn.ScreenType = Data1;
  186.                theMessage.Data.OpenOn.SizeToFit  = Data2;
  187.                break;
  188.  
  189. #ifdef WICONIFY_PRIVATE
  190.             case WI_ENDICONIFY:
  191.                theMessage.Data.DataPtr = FindTask(NULL);
  192.                break;
  193. #endif
  194. #endif
  195. #endif
  196.          }
  197.          theMessage.Message.mn_ReplyPort = thePort;
  198.          theMessage.Message.mn_Length = sizeof(struct wIconMessage);
  199.          PutMsg(wIconPort,&theMessage);
  200.          WaitPort(thePort);
  201.          DeletePort(thePort);
  202.       }
  203.    }
  204.    if (theAction == WI_BACKDROPOF)
  205.       theMessage.Icon = (WICONREF *)theMessage.Window;
  206. #ifndef WINDOW_CALLS
  207. #ifndef ICONSETTER_CALLS
  208.    else if (theAction == WI_NEWSCREEN)
  209.       theMessage.Icon = (WICONREF *)theMessage.Data.Screen;
  210. #endif
  211. #endif
  212.    return(theMessage.Icon);
  213. }
  214.  
  215.  
  216. /*
  217.  *  The remainder of the routines simply call SendMessage to send
  218.  *  the appropriate message type with the appropriate data, and
  219.  *  return a pointer to a wIconRef when necessary
  220.  */
  221.  
  222.  
  223. #ifndef SCREEN_CALLS
  224. /*
  225.  *  wIconify()
  226.  *  wIconifyScreen()
  227.  *
  228.  *  Attempt to iconify the specified window or screen (iconifying
  229.  *  the backdrop window iconifies the screen).
  230.  */
  231.  
  232. WICONREF *wIconify(theWindow)
  233. struct Window *theWindow;
  234. {
  235.    return(SendMessage(WI_ICONIFY,theWindow,NULL));
  236. }
  237.  
  238. WICONREF *wIconifyScreen(theScreen)
  239. struct Screen *theScreen;
  240. {
  241.    return(wIconify(wBackDropOf(theScreen)));
  242. }
  243.  
  244.  
  245. /*
  246.  *  wIconOf()
  247.  *  wIconOfScreen()
  248.  *
  249.  *  Returns the wIconRef for the icon of the specified window or screen
  250.  *  (the screen icon is stored as the icon of the backdrop window).
  251.  */
  252.  
  253. WICONREF *wIconOf(theWindow)
  254. struct Window *theWindow;
  255. {
  256.    return(SendMessage(WI_ICONOF,theWindow,NULL));
  257. }
  258.  
  259. #ifndef ICONSETTER_CALLS
  260.  
  261. WICONREF *wIconOfScreen(theScreen)
  262. struct Screen *theScreen;
  263. {
  264.    return(wIconOf(wBackDropOf(theScreen)));
  265. }
  266.  
  267.  
  268. /*
  269.  *  wRestore()
  270.  *
  271.  *  Attempt to restore the window of the given wIconRef.
  272.  */
  273.  
  274. void wRestore(theIcon)
  275. WICONREF *theIcon;
  276. {
  277.    SendMessage(WI_RESTORE,NULL,theIcon);
  278. }
  279.  
  280.  
  281. /*
  282.  *  wGetIconData()
  283.  *
  284.  *  Copy the data from the wIconRef into the wIcon structure.
  285.  *  (Use with wUpdateIcon() to modify an icon).
  286.  */
  287.  
  288. void wGetIconData(theIcon,theIconRef)
  289. WICON *theIcon;
  290. WICONREF *theIconRef;
  291. {
  292.    if (theIcon && theIconRef)
  293.       Forbid(), *theIcon = theIconRef->Icon, Permit();
  294. }
  295. #endif
  296.  
  297.  
  298. /*
  299.  *  *wSetIcon()
  300.  *  *wSetScreenIcon()
  301.  *
  302.  *  Assign the given icon definition to the specified window or screen, and
  303.  *  return the wIconRef pointer of the resulting icon.
  304.  */
  305.  
  306. WICONREF *wSetIcon(theWindow,theIcon)
  307. struct Window *theWindow;
  308. WICON *theIcon;
  309. {
  310.    return(SendMessage(WI_SETICON,theWindow,theIcon));
  311. }
  312.  
  313. WICONREF *wSetScreenIcon(theScreen,theIcon)
  314. struct Screen *theScreen;
  315. WICON *theIcon;
  316. {
  317.    return(wSetIcon(wBackDropOf(theScreen),theIcon));
  318. }
  319.  
  320.  
  321. #ifndef WINDOW_CALLS
  322. #ifndef ICONSETTER_CALLS
  323. /*
  324.  *  wUnSetIcon()
  325.  *  wUnSetScreenIcon()
  326.  *
  327.  *  Clear any icon associated with the window or screen.
  328.  */
  329.  
  330. void wUnSetIcon(theWindow)
  331. struct Window *theWindow;
  332. {
  333.    SendMessage(WI_UNSETICON,theWindow,NULL);
  334. }
  335.  
  336. void wUnSetScreenIcon(theScreen)
  337. struct Screen *theScreen;
  338. {
  339.    wUnSetIcon(wBackDropOf(theScreen));
  340. }
  341.  
  342.  
  343. /*
  344.  *  wUpdateIcon()
  345.  *
  346.  *  Update the values of the wIconRef to those specified in the wIcon.
  347.  */
  348.  
  349. void wUpdateIcon(theIcon,WIcon)
  350. WICONREF *theIcon;
  351. WICON *WIcon;
  352. {
  353.    SendMessage(WI_UPDATEICON,NULL,theIcon,WIcon);
  354. }
  355.  
  356. /*
  357.  *  wSelectIcon()
  358.  *
  359.  *  Attempt to select the given icon.  If AddToSelect is TRUE, this is
  360.  *  a multiple selection, otherwise, any previously selected items are
  361.  *  deselected first.
  362.  */
  363.  
  364. void wSelectIcon(theIcon,AddToSelect)
  365. WICONREF *theIcon;
  366. long AddToSelect;
  367. {
  368.    SendMessage(WI_SELECTICON,NULL,theIcon,AddToSelect);
  369. }
  370.  
  371.  
  372. /*
  373.  *  wUnSelectIcon()
  374.  *
  375.  *  Make the given icon become unselected.
  376.  */
  377.  
  378. void wUnSelectIcon(theIcon)
  379. WICONREF *theIcon;
  380. {
  381.    SendMessage(WI_UNSELECT,NULL,theIcon);
  382. }
  383.  
  384.  
  385. /*
  386.  *  wMoveIcon()
  387.  *
  388.  *  Move the icon to the specified location.
  389.  */
  390.  
  391. void wMoveIcon(theIcon,x,y)
  392. WICONREF *theIcon;
  393. WORD x,y;
  394. {
  395.    SendMessage(WI_MOVEICON,NULL,theIcon,x,y);
  396. }
  397. #endif
  398.  
  399. /*
  400.  *  wIconXY()
  401.  *
  402.  *  Gets the position of the given icon and places them in X and Y.
  403.  */
  404.  
  405. void wIconXY(theIcon,X,Y)
  406. WICONREF *theIcon;
  407. WORD *X,*Y;
  408. {
  409.    Forbid();
  410.    if (theIcon)
  411.    {
  412.       if (theIcon->Gadget)
  413.       {
  414.          *X = theIcon->Gadget->Gadget.LeftEdge;
  415.          *Y = theIcon->Gadget->Gadget.TopEdge;
  416.       } else {
  417.          *X = theIcon->Icon.x;
  418.          *Y = theIcon->Icon.y;
  419.       }
  420.    }
  421.    Permit();
  422. }
  423.  
  424. #ifndef ICONSETTER_CALLS
  425. /*
  426.  *  wModifyFlags()
  427.  *
  428.  *  Update the icon's Flags field to the given flags.
  429.  */
  430.  
  431. void wModifyFlags(theIcon,Flags)
  432. WICONREF *theIcon;
  433. ULONG Flags;
  434. {
  435.    if (theIcon)
  436.    {
  437.       Forbid();
  438.       theIcon->Icon.Flags &= WI_SYSTEMFLAGS;
  439.       theIcon->Icon.Flags |= (Flags & ~WI_SYSTEMFLAGS);
  440.       Permit();
  441.    }
  442. }
  443.  
  444.  
  445. /*
  446.  *  wModifyReport()
  447.  *
  448.  *  Modify the icon's Report field to the given classes.
  449.  */
  450.  
  451. void wModifyReport(theIcon,Report)
  452. WICONREF *theIcon;
  453. ULONG Report;
  454. {
  455.    if (theIcon) Forbid(), theIcon->Icon.Report = Report, Permit();
  456. }
  457.  
  458.  
  459. /*
  460.  *  wRefreshIcon()
  461.  *
  462.  *  Redraw all the icons on the screen containing the given icon.
  463.  */
  464.  
  465. void wRefreshIcon(theIcon)
  466. WICONREF *theIcon;
  467. {
  468.    if (theIcon) SendMessage(WI_REDRAW,NULL,NULL,theIcon->Screen);
  469. }
  470.  
  471.  
  472. /*
  473.  *  *wAddIcon()
  474.  *
  475.  *  Add an icon (with no associated window) to the given screen.
  476.  *  Returns the wIconRef for the newly created icon.
  477.  */
  478.  
  479. WICONREF *wAddIcon(theScreen,theIcon)
  480. struct Screen *theScreen;
  481. WICON *theIcon;
  482. {
  483.    return(SendMessage(WI_ADDICON,NULL,theIcon,theScreen));
  484. }
  485.  
  486.  
  487. /*
  488.  *  wRemoveIcon()
  489.  *
  490.  *  Remove an icon created by wAddIcon().
  491.  */
  492.  
  493. void wRemoveIcon(theIcon)
  494. WICONREF *theIcon;
  495. {
  496.    SendMessage(WI_REMOVEICON,NULL,theIcon);
  497. }
  498.  
  499.  
  500. /*
  501.  *  wCloseIcon()
  502.  *
  503.  *  Close the window associated with a given icon.
  504.  *  The same as choosing CLOSE from the ICON menu.
  505.  */
  506.  
  507. void wCloseIcon(theIcon)
  508. WICONREF *theIcon;
  509. {
  510.    SendMessage(WI_CLOSEICON,NULL,theIcon);
  511. }
  512.  
  513.  
  514. /*
  515.  *  wNoIconify()
  516.  *
  517.  *  Mark the given window as not-iconifiable.  Any attempt by the user
  518.  *  to iconify the window will fail.
  519.  */
  520.  
  521. void wNoIconify(theWindow)
  522. struct Window *theWindow;
  523. {
  524.    WICONREF *theIcon;
  525.    
  526.    theIcon = wIconOf(theWindow);
  527.    if (theIcon == NULL) theIcon = wSetIcon(theWindow,NULL);
  528.    if (theIcon) Forbid(), theIcon->Icon.Flags |= WI_NOICONIFY, Permit();
  529. }
  530.  
  531.  
  532. /*
  533.  *  wIconReport()
  534.  *
  535.  *  Returns the current settings of the icon's Report field.
  536.  */
  537.  
  538. ULONG wIconReport(theIcon)
  539. WICONREF *theIcon;
  540. {
  541.    ULONG Report = 0;
  542.  
  543.    if (theIcon) Forbid(), Report = theIcon->Icon.Report, Permit();
  544.    return(Report);
  545. }
  546. #endif
  547. #endif
  548.  
  549.  
  550. #ifndef ICONSETTER_CALLS
  551. /*
  552.  *  wIconFlags()
  553.  *
  554.  *  Returns the current contents of the icon's Flags field.
  555.  */
  556.  
  557. ULONG wIconFlags(theIcon)
  558. WICONREF *theIcon;
  559. {
  560.    ULONG Flags = 0;
  561.  
  562.    if (theIcon) Forbid(), Flags = theIcon->Icon.Flags, Permit();
  563.    return(Flags);
  564. }
  565.  
  566.  
  567. /*
  568.  *  wIsIconified()
  569.  *  wIsScreenIconified()
  570.  *
  571.  *  Returns TRUE if the given window or screen is iconified,
  572.  *  and FALSE otherwise.
  573.  */
  574.  
  575. int wIsIconified(theWindow)
  576. struct Window *theWindow;
  577. {
  578.    return((wIconFlags(wIconOf(theWindow)) & WI_ICONIFIED) != FALSE);
  579. }
  580.  
  581. int wIsScreenIconified(theScreen)
  582. struct Screen *theScreen;
  583. {
  584.    return(wIsIconified(wBackDropOf(theScreen)));
  585. }
  586.  
  587.  
  588. #ifndef WINDOW_CALLS
  589. /*
  590.  *  wWindowOf()
  591.  *
  592.  *  Returns a pointer to the window associated with a given icon.
  593.  */
  594.  
  595. struct Window *wWindowOf(theIcon)
  596. WICONREF *theIcon;
  597. {
  598.    struct Window *theWindow = NULL;
  599.    
  600.    if (theIcon) Forbid(), theWindow = theIcon->Window, Permit();
  601.    return(theWindow);
  602. }
  603. #endif
  604. #endif
  605. #endif
  606.  
  607.  
  608. /*
  609.  *  *wBackDropOf()
  610.  *
  611.  *  Returns a pointer to the wIconify window of the given screen.  If there
  612.  *  is no wIconify window on the screen, NULL is returned.
  613.  */
  614.  
  615. struct Window *wBackDropOf(theScreen)
  616. struct Screen *theScreen;
  617. {
  618.    return((struct Window *)SendMessage(WI_BACKDROPOF,NULL,NULL,theScreen));
  619. }
  620.  
  621.  
  622. /*
  623.  *  These routines are used by the wIconify utilities, such as wIconifyWindow,
  624.  *  wIconScreen, wOpenOn, and wMakeWB, to do their thing.  User programs
  625.  *  should not use these routines, in general.
  626.  */
  627.  
  628.  
  629. #ifdef WKEYS_CALLS
  630. /*
  631.  *  wSelectNext()
  632.  *
  633.  *  Attempt to select the next icon on the given screen.
  634.  */
  635.  
  636. void wSelectNext(theScreen)
  637. struct Screen *theScreen;
  638. {
  639.    SendMessage(WI_SELECTNEXT,NULL,NULL,theScreen);
  640. }
  641.  
  642.  
  643. /*
  644.  *  wOpenSelected()
  645.  *
  646.  *  Open the selected icons on the given screen.
  647.  *  Same as choosing OPEN from the ICON menu.
  648.  */
  649.  
  650. void wOpenSelected(theScreen)
  651. struct Screen *theScreen;
  652. {
  653.    SendMessage(WI_OPENSELECT,NULL,NULL,theScreen);
  654. }
  655.  
  656.  
  657. /*
  658.  *  wCloseSelected()
  659.  *
  660.  *  Close the selected icons on the given screen.
  661.  *  Same as choosing CLOSE from the ICON menu.
  662.  */
  663.  
  664. void wCloseSelected(theScreen)
  665. struct Screen *theScreen;
  666. {
  667.    SendMessage(WI_CLOSESELECT,NULL,NULL,theScreen);
  668. }
  669. #endif
  670.  
  671.  
  672. #ifdef SCREEN_CALLS
  673. /*
  674.  *  *wNewScreen()
  675.  *
  676.  *  Open a new screen of the given depth an modes, and return a pointer
  677.  *  to the new screen, or NULL if none created.
  678.  *  (For use by wIconScreen).
  679.  */
  680.  
  681. struct Screen *wNewScreen(Depth,Modes)
  682. UWORD Depth,Modes;
  683. {
  684.    return((struct Screen *)SendMessage(WI_NEWSCREEN,NULL,NULL,Depth,Modes));
  685. }
  686.  
  687.  
  688. /*
  689.  *  wMakeWB()
  690.  *
  691.  *  Change the current WB screen to the given screen.
  692.  *  (For use by wMakeWB).
  693.  */
  694.  
  695. void wMakeWB(theScreen)
  696. struct Screen *theScreen;
  697. {
  698.    SendMessage(WI_MAKEWB,NULL,NULL,theScreen);
  699. }
  700.  
  701.  
  702. /*
  703.  *  wSetOpenOn()
  704.  *
  705.  *  Set the OpenOn submenu to the specified type of window and sizing option.
  706.  *  (For use by wOpenOn).
  707.  */
  708.  
  709. void wSetOpenOn(ScreenType,SizeToFit)
  710. int ScreenType,SizeToFit;
  711. {
  712.    SendMessage(WI_OPENON,NULL,NULL,ScreenType,SizeToFit);
  713. }
  714. #endif
  715.  
  716.  
  717. #ifdef WICONIFY_PRIVATE
  718. /*
  719.  *  wEndIconify()
  720.  *
  721.  *  Ask wIconify to terminate.  This is for the internal use of wIconify ONLY!
  722.  */
  723.  
  724. APTR wEndIconify()
  725. {
  726.    return((APTR)SendMessage(WI_ENDICONIFY,NULL,NULL));
  727. }
  728. #endif
  729.